package com.TechM.get.Service;


import java.util.ArrayList;
import java.util.List;
//import java.text.SimpleDateFormat;
//import java.util.Date;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.TechM.get.GetData;
import com.TechM.get.UuidUtil;
import com.TechM.get.Dao.QueryDataRepository;
import com.TechM.get.Domain.MDriveMotionInfo;
import com.TechM.get.Domain.MVehicleLocation;
import com.TechM.get.Domain.MVehicleMeterInfoM;
import com.TechM.get.Domain.MVehicleTrip;
import com.TechM.get.Domain.QueryData;

@Service
public class QueryDataService {
	@Autowired
	QueryDataRepository queryDataRepository;
	//将查询到的数据转成string类型存到数据库
	public void saveData() {
		//存储需要查询的deviceID
		List<String> deviceIDs = new ArrayList<String>();	
		deviceIDs.add("14dec7f6-76cd-4f05-8bba-6b0710c0c345");
		deviceIDs.add("b2f2d896-4f00-44dc-beb4-64fde9891631");
		deviceIDs.add("0ec09c91-6f93-41e8-a59a-54cc3eb8dacd");
		deviceIDs.add("3490dbd6-59d1-4a7b-9f76-95fa7c0d1ace");
		
//		generatedVehicleSQL(deviceIDs);
		
		GetData getData = new GetData();
		
		System.out.println("generatedVehicleLocationSQL ========================================");
		System.out.println("generatedVehicleMeterInfoSQL ========================================");
		
		//遍历deviceID查询数据并存储
		for (String deviceID : deviceIDs) {
			Map<String, Object> data = getData.query(deviceID);
			if(data != null){
				QueryData queryData = setQueryData(data);
				
				//生成sql
				generatedVehicleLocationSQL(queryData);
				generatedVehicleMeterInfoSQL(queryData);
				generatedDriveMotionInfoSQL(queryData);
				generatedMVehicleTripSQL(queryData);
//				queryDataRepository.save(queryData);
			}else{
				System.out.println("Query Data = null");
			}
		}
	}
	
	public static void main(String[] args) {
		List<String> deviceIDs = new ArrayList<String>();	
		deviceIDs.add("14dec7f6-76cd-4f05-8bba-6b0710c0c345");
		deviceIDs.add("b2f2d896-4f00-44dc-beb4-64fde9891631");
		deviceIDs.add("0ec09c91-6f93-41e8-a59a-54cc3eb8dacd");
		deviceIDs.add("3490dbd6-59d1-4a7b-9f76-95fa7c0d1ace");
		
		QueryDataService qds = new QueryDataService();
		qds.generatedVehicleSQL(deviceIDs);
	}
	
	
	/**
	 * 生成车辆信息
	 * @param deviceIDs
	 */
	public void generatedVehicleSQL(List<String> deviceIDs){
		
		System.out.println("generatedVehicleSQL ========================================");
		
		
		for (int i = 0 ; i < deviceIDs.size() ; i ++) {
			String m_vehicle_info_sql = "INSERT INTO \"public\".\"m_vehicle_info\" "
					+ "(\"id\", \"card_no\", \"vin\", \"sim_no\", \"term_id\", \"iccid\", \"imei\", \"create_time\", \"device_id\") "
					+ "VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');";
			
			String id = UuidUtil.creatUUID();
			//车牌号码
			String cardNo = "沪A1234" + i;
			//车辆识别码
			String vin = i + "";
			//SIM卡号码
			String simNo = i + "";
			//终端ID
			String termId = i + "";
			//SIM卡卡号
			String iccid = i + "";
			//国际移动装备辨识码
			Integer imei = i;
			//创建时间
			String createTime = "now()";
			//device_id
			String deviceId = deviceIDs.get(i);
			
			
			System.out.println(String.format(m_vehicle_info_sql,id , cardNo, vin, simNo, termId, iccid, imei, createTime, deviceId));
		}
	}
	
	/**
	 * 生成车辆位置Sql
	 * @param queryData
	 */
	public void generatedVehicleLocationSQL(QueryData queryData){
		String vehicle_location_sql = "INSERT INTO \"public\".\"m_vehicle_location\" "
				+ "(\"vehicle_id\", \"longitude\", \"latitude\", \"height\", \"direction\", \"accuracy\", \"gnss_no\", \"available\", \"create_time\")"
				+ " select id, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' from m_vehicle_info where device_id='%s';";
		
		MVehicleLocation vehicleLocation = new MVehicleLocation();
		vehicleLocation.setLongitude(queryData.getLongitude());
		vehicleLocation.setLatitude(queryData.getLatitude());
		vehicleLocation.setHeight(queryData.getAltitude());
		vehicleLocation.setDirection(Integer.parseInt(queryData.getDirection() == null ? "3" : queryData.getDirection()));
		//TODO 获取到的数据是double
		vehicleLocation.setAccuracy(3);
		vehicleLocation.setGnssNo(3);
		vehicleLocation.setAvailable(Integer.parseInt(queryData.getAvailable() == null ? "3" : queryData.getAvailable()));
		vehicleLocation.setCreateTime("now()");
		
		System.out.println(String.format(vehicle_location_sql, 
				vehicleLocation.getLongitude(),
				vehicleLocation.getLatitude(), vehicleLocation.getHeight(), vehicleLocation.getDirection(),
				vehicleLocation.getAccuracy(), vehicleLocation.getGnssNo(),
				vehicleLocation.getAvailable(), vehicleLocation.getCreateTime(),
				queryData.getDeviceId()));
	}
	
	
	
	/**
	 * 生成车辆仪表信息Sql
	 * @param queryData
	 */
	public void generatedVehicleMeterInfoSQL(QueryData queryData){
		String vehicle_meter_info_sql = "INSERT INTO \"public\".\"m_vehicle_meter_info_m\" "
				+ "(\"id\", \"vehicle_id\", \"appr_type\", \"appr_volumn\", \"create_time\") "
				+ " select '%s', id, '%s', '%s', '%s' from m_vehicle_info where device_id='%s';";
		MVehicleMeterInfoM vmim = new MVehicleMeterInfoM();
		vmim.setId(UuidUtil.creatUUID());
		vmim.setApprType(1);
		vmim.setApprVolumn(queryData.getMileage());
		vmim.setCreateTime("now()");
		System.out.println(String.format(vehicle_meter_info_sql, vmim.getId(), vmim.getApprType(),
				vmim.getApprVolumn(), vmim.getCreateTime(), queryData.getDeviceId()));
	}
	
	/**
	 * 生成 车驾驶行为表 sql
	 * @param queryData
	 */
	public void generatedDriveMotionInfoSQL(QueryData queryData){
		if (!StringUtils.isEmpty(queryData.getAlarmType())){
			
			String drive_motion_info_sql = "INSERT INTO \"public\".\"m_drive_motion_info\" "
					+ "(\"id\", \"vehicle_id\", \"alarmtype\", \"create_time\")"
					+ " select '%s', id, '%s', '%s' from m_vehicle_info where device_id='%s';";
			MDriveMotionInfo mdmi = new MDriveMotionInfo();
			mdmi.setId(UuidUtil.creatUUID());
			mdmi.setAlarmtype(queryData.getAlarmType());
			mdmi.setCreateTime("now()");
			
			System.out.println(String.format(drive_motion_info_sql, mdmi.getId(),
					mdmi.getAlarmtype(), mdmi.getCreateTime(), queryData.getDeviceId()));
			
		}
	}
	
	/**
	 * 生成 车辆旅程信息 sql
	 * @param queryData
	 */
	public void generatedMVehicleTripSQL(QueryData queryData){
		String vehicle_trip_sql = "INSERT INTO \"public\".\"m_vehicle_trip\" "
				+ "(\"trip_num\", \"trip_status\", \"vid\", \"create_time\", \"longitude\", \"latitude\", \"total_mile\")"
				+ " select '%s', '%s', id, '%s', '%s', '%s', '%s' from m_vehicle_info where device_id='%s';";
		
		MVehicleTrip vt = new MVehicleTrip();
		vt.setTripNum(queryData.getTripNo());
		vt.setTripStatus(Integer.parseInt(queryData.getTripStatus() == null ? "1" : queryData.getTripStatus()));
		vt.setCreateTime("now()");
		vt.setLongitude(queryData.getLongitude());
		vt.setLatitude(queryData.getLatitude());
		vt.setTotalMile(Double.valueOf(queryData.getMileage() == null ? "0" : queryData.getMileage()).intValue());
		
		System.out.println(String.format(vehicle_trip_sql, vt.getTripNum(),
				vt.getTripStatus(), vt.getCreateTime(),
				vt.getLongitude(), vt.getLatitude(),
				vt.getTotalMile(), queryData.getDeviceId()));
		
	}
	
	
	
	private QueryData setQueryData(Map<String, Object> data) {
		String deviceId = data.get("deviceId").toString();
		String latitude = data.get("latitude").toString();
		String longitude = data.get("longitude").toString();
		String altitude = data.get("altitude").toString();
		Integer speed = (Integer) data.get("speed");
		String direction = data.get("direction").toString();
		String time = data.get("time").toString();
		String accuracy = data.get("accuracy").toString();
		String rpm = data.get("rpm").toString();
		String alarmType = data.get("alarmType").toString();
		String available = data.get("available").toString();
		String accStatus = data.get("accStatus").toString();
		String mileage = data.get("mileage").toString();
		String tripNo = data.get("tripNo").toString();
		String tripStatus = data.get("tripStatus").toString();
		String occurTime = data.get("occurTime").toString();
		String transTime = data.get("transTime").toString();
		String noSignalResend = data.get("noSignalResend").toString();
		QueryData queryData = new QueryData();
		queryData.setDeviceId(deviceId);
		queryData.setLatitude(latitude);
		queryData.setLongitude(longitude);
		queryData.setAltitude(altitude);
		queryData.setSpeed(speed);
		queryData.setDirection(direction);
		queryData.setTime(time);
		queryData.setAccuracy(accuracy);
		queryData.setRpm(rpm);
		queryData.setAlarmType(alarmType);
		queryData.setAvailable(available);
		queryData.setAccStatus(accStatus);
		queryData.setMileage(mileage);		
		queryData.setTripNo(tripNo);
		queryData.setTripStatus(tripStatus);
		queryData.setOccurTime(occurTime);
		queryData.setTransTime(transTime);
		queryData.setNoSignalResend(noSignalResend);
		//SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");			
		//queryData.setCurrTime(df.format(new Date()));
		return queryData;
	}
}
